package de.eridea.learning.java.usermanagement.logic.exception;

import java.util.Iterator;
import java.util.Set;

/**
 * Thrown when saving a database entry fails because required properties are missing.
 * 
 * @author frank
 *
 */
public class InsufficientDataException extends UserManagementException {

	/**
	 * The names of the properties which need to be set.
	 */
	private Set<String> missingProperties;
	
	private static final long serialVersionUID = -2362063614471164472L;

	public Set<String> getMissingProperties() {
		return missingProperties;
	}

	public InsufficientDataException(Set<String> missingProperties) {
		super(message(missingProperties));
		this.missingProperties = missingProperties;
	}

	public InsufficientDataException(String message, Set<String> missingProperties) {
		super(message(message, missingProperties));
		this.missingProperties = missingProperties;
		
	}

	public InsufficientDataException(Throwable cause, Set<String> missingProperties) {
		super(message(missingProperties), cause);
		this.missingProperties = missingProperties;
		
	}

	public InsufficientDataException(String message, Throwable cause, Set<String> missingProperties) {
		super(message(message, missingProperties), cause);
		this.missingProperties = missingProperties;
	}

	private static String message(String message, Set<String> missingProperties) {
		return message + " ("+message(missingProperties)+")";
	}
	
	private static String message(Set<String> missingProperties) {
		return "Missing properties: "+listOfMissingProperties(missingProperties); 
	}
	
	private static String listOfMissingProperties(Set<String> missingProperties) {
        StringBuffer listString = new StringBuffer();
        String delim = ", ";
        Iterator<String> i = missingProperties.iterator();
        if (i.hasNext()) {
        	listString.append(i.next());
        	while (i.hasNext()) {
        		listString.append(delim);
        		listString.append(i.next());
        	}
        }
		return listString.toString();
	}
}
